package com.acme.bankapp.service.bank;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.client.Gender;
import com.acme.bankapp.exceptions.ClientExistsException;
import com.acme.bankapp.exceptions.NotEnoughFundsException;
import com.acme.bankapp.networking.server.BankServer;

import java.io.File;
import java.io.IOException;

public class BankApplication {

    public static void main(String... args) {

        String feedFileName = "";
        if ((args.length >= 2) && (args[0].equals("-loadbank")))
            feedFileName = args[1];

        Bank bank;
        try {
            File feedFile = new File(feedFileName);
            if (feedFile.exists()) {
                bank = BankService.readBank(feedFile);
                BankService.modifyClient(bank, "Smith", 300);
            } else {
                bank = new Bank();
                BankService.addClient(bank, "Smith", Gender.MALE, 200, 0);
                /*BankService.addClient(bank, "John", Gender.MALE, 100, 300);
                BankService.addClient(bank, "Sara", Gender.FEMALE, 400, 0);
                BankService.addClient(bank, "Sara", Gender.MALE, 400, 100);

                BankService.modifyClient(bank, "Sara", 900);*/
            }

            for (String arg : args)
                if (arg.equals("-server")) {
                    BankServer server = new BankServer(bank);
                    server.runServer();
                }

        } catch (IllegalArgumentException | NotEnoughFundsException | IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } catch (ClientExistsException e) {
            System.err.println("Client " + e.getMessage() + " already exists");
        }
    }
}
